package com.ayuer.chapter03;

public class RemLastEleLinkListDemo {

    static class ListNode {
        private int val;
        private ListNode next;

    }


    public static void main(String[] args) {


    }


    /**
     * 使用的是快慢指针法
     * @param head
     * @param index
     * @return
     */
    public ListNode removeLast(ListNode head, int index) {

        //使用哨兵节点
        ListNode sentinel = new ListNode();
        ListNode p1 = sentinel;
        ListNode p2 = sentinel;
        //初始化指针
        for (int i = 0; i <= index + 1; i++) {
            p2 = p2.next;
        }

        //不为空就向后平移
        while (p2 != null) {
            p1 = p1.next;
            p2 = p2.next;
        }

        p1.next = p1.next.next;

        return null;
    }


}
